import { ModelAttributes, DataTypes, InitOptions, Sequelize } from 'sequelize'

export class ModelUtils {
  private static readonly AUTO_INCREAM: string = '自增主键'
  private static readonly CREATE_TIME: string = '创建时间'
  private static readonly UPDATE_TIME: string = '更新时间'
  private static readonly CREATOR: string = '创建者'
  private static readonly UPDATER: string = '更新者'
  private static readonly TIMESTAMP: string = 'TIMESTAMP'
  static appendPublicFields(fields: ModelAttributes): ModelAttributes {
    fields.id = { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, comment: this.AUTO_INCREAM }
    fields.createTime = { type: this.TIMESTAMP, allowNull: false, comment: this.CREATE_TIME }
    fields.updateTime = { type: this.TIMESTAMP, allowNull: false, defaultValue: DataTypes.NOW, comment: this.UPDATE_TIME }
    fields.creator = { type: DataTypes.STRING, allowNull: false, comment: this.CREATOR }
    fields.updater = { type: DataTypes.STRING, allowNull: false, comment: this.UPDATER }
    return fields
  }
  static appendTablePublicProperties(tableName: string, sequelizeInstance: Sequelize, comment?: string): InitOptions {
    return {
      tableName: tableName,
      comment: comment,
      collate: 'utf8_general_ci',
      sequelize: sequelizeInstance,
      timestamps: true,
      createdAt: 'createTime',
      updatedAt: 'updateTime'
    }
  }
}